package com.example.ggpicturegg.first;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.ggpicturegg.Mysql_com.ImageDao;
import com.example.ggpicturegg.Mysql_com.ImageInfo;
import com.example.ggpicturegg.bottom.Friend_b;
import com.example.ggpicturegg.bottom.Myinfo;
import com.example.ggpicturegg.bottom.Publish_b;
import com.example.ggpicturegg.bottom.Talk_b;
import com.example.ggpicturegg.ftpUD.UpAndDown;
import com.example.ggpicturegg.myAdapter.MyAdapter;
import com.example.ggpicturegg.R;
import com.example.ggpicturegg.tool.BlobAndBytes;
import com.example.ggpicturegg.tool.Loaddata;

import java.io.InputStream;
import java.util.List;

public class RecyclerviewActivity extends AppCompatActivity {

    private RecyclerView mRvGrid;
    private TextView tv_like;
    private TextView tv_care;

    private  TextView friend;
    private  TextView talk;
    private  TextView myinfo;
    private ImageView im_add_x;
    private int ans=0;


    private List<ImageInfo> image_list;       // 照片集合
    private ImageDao imageDao;  // image 操作实列
    private Handler mainHandler;
    private Handler mainHandler_load; //加载数据
    private MyAdapter myAdapter;
    private byte[][] bytes;

    Intent in = getIntent();
    Bundle bundle1;
    String sss=null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_recyclerview);

        initView();
        transinfo();
        loadImagedb();     //加载图片数据


    }

    private void transinfo(){
        bundle1 = getIntent().getExtras();
        sss = bundle1.getString("name");
        if(sss!=null){Log.d("--Re接受 Login 登录成功的值-- ", "-  is         " +sss );}
        if(sss==null){
            sss = bundle1.getString("public_ok_to_Re_name");   // 接受 public 上传成功的值
            if(sss!=null){ Log.d("--Re接受publish 上传成功的值-- ", "-  is         " +sss );}
        }
        if(sss==null){
            sss = bundle1.getString("public_return_name");   // 接受 public return成功的值
            if(sss!=null){Log.d("--Re接受publish 返回成功的值-- ", "-  is         " + sss );}
        }
        if(sss==null){
            sss = bundle1.getString("myinfo_to_Re");   // 接受 public return成功的值
            if(sss!=null){ Log.d("--Re接受 myinfo 返回成功的值-- ", "-  is         " + sss );}
        }
        if(sss==null){
            sss = bundle1.getString("talk_to_Re");   // 接受 public return成功的值
            if(sss!=null){Log.d("--Re接受 talk 返回成功的值-- ", "-  is         " + sss );}
        }
        if(sss==null){
            sss = bundle1.getString("friend_to_Re");   // 接受 public return成功的值
            if(sss!=null){Log.d("--Re接受 Friend 返回成功的值-- ", "-  is         " + sss );}
        }
        if(sss==null){
            sss = bundle1.getString("like_to_Re");   // 接受 public return成功的值
            if(sss!=null){ Log.d("--Re接受 Like 返回成功的值-- ", "-  is         " + sss );}
        }
        if(sss==null){
            sss = bundle1.getString("care_to_Re");   // 接受 public return成功的值
            if(sss!=null){Log.d("--Re接受 Care 返回成功的值-- ", "-  is         " + sss );}
        }
    }

    private void initView(){
        im_add_x = findViewById(R.id.add_x);
        tv_like = findViewById(R.id.like);
        tv_care = findViewById(R.id.care);
        talk = findViewById(R.id.talk_x);
        friend = findViewById(R.id.friend_x);
        myinfo = findViewById(R.id.myinfo_x);
        OnClick onClick = new OnClick();
        tv_like.setOnClickListener(onClick);
        tv_care.setOnClickListener(onClick);
        talk.setOnClickListener(onClick);
        friend.setOnClickListener(onClick);
        myinfo.setOnClickListener(onClick);
        mRvGrid = (RecyclerView)findViewById(R.id.rv_grid);
        mRvGrid.setLayoutManager(new GridLayoutManager(RecyclerviewActivity.this,2));
        im_add_x.setOnClickListener(onClick);
        imageDao = new ImageDao();
        mainHandler = new Handler(getMainLooper()) ; //获取主线程

    }


    //加载数据
    private void loadImagedb(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                ans = imageDao.getImage_max_id();
                image_list = imageDao.getAllUserList();  //获取全部用户数据

                String sss = new String(String.valueOf(image_list.size()));
                 Log.d("用户个数。。。。。。。", "----------------   is    "+sss);

                for(int i=0;i<image_list.size();i++){
                    Bitmap bitmap =  Loaddata.getImage(image_list.get(i).getPicture_name(),image_list.get(i).getId());
                   // System.out.println("-----123---------- "+image_list.get(i).getPicture_name()+" -------------------------");

                    int finalI = i;
                    mainHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            image_list.get(finalI).setBitmap(bitmap);
                            showListViewData();
                        }
                    });
                }

            }
        }).start();
    }
    //显示数据
    private void showListViewData(){
        if(myAdapter==null){
            myAdapter = new MyAdapter(RecyclerviewActivity.this, image_list, new MyAdapter.OnItemClickListener() {
                @Override
                public void OnClick(int pos) {
                    Toast.makeText(RecyclerviewActivity.this,"click"+pos,Toast.LENGTH_SHORT).show();
                }
            },sss);
            mRvGrid.setAdapter(myAdapter);
        }
        else{
            myAdapter.setImage_list(image_list);
            myAdapter.notifyDataSetChanged();
        }
    }



    class OnClick implements View.OnClickListener{
        private  Intent intent;
        Bundle bundle;

        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.like:
                    intent = new Intent(RecyclerviewActivity.this,Like_list.class);
                    Log.d("--Re 发送到 like 的值-- ", "--  is         " +sss );
                    bundle = new Bundle();
                    //bundle.putInt("max_id",ans);
                    bundle.putString("Re_to_like",sss);
                    intent.putExtras(bundle);
                    startActivity(intent);
                    break;
                case R.id.care:
                    intent = new Intent(RecyclerviewActivity.this,Care_list.class);
                    Log.d("--Re 发送到 Care 的值-- ", "--  is         " +sss );
                    bundle = new Bundle();
                    //bundle.putInt("max_id",ans);
                    bundle.putString("Re_to_care",sss);
                    intent.putExtras(bundle);
                    startActivity(intent);
                    break;
                case R.id.talk_x:
                    intent = new Intent(RecyclerviewActivity.this, Talk_b.class);
                    Log.d("--Re 发送到 Talk 的值-- ", "--  is         " +sss );
                    bundle = new Bundle();
                    //bundle.putInt("max_id",ans);
                    bundle.putString("Re_to_talk",sss);
                    intent.putExtras(bundle);
                    startActivity(intent);
                    break;
                case R.id.friend_x:
                    intent = new Intent(RecyclerviewActivity.this, Friend_b.class);
                    Log.d("--Re 发送到 Friend 的值-- ", "--  is         " +sss );
                    bundle = new Bundle();
                    //bundle.putInt("max_id",ans);
                    bundle.putString("Re_to_friend",sss);
                    intent.putExtras(bundle);
                    startActivity(intent);
                    break;
                case R.id.myinfo_x:
                    intent = new Intent(RecyclerviewActivity.this, Myinfo.class);
                    Log.d("--Re 发送到 MyInfo 的值-- ", "--  is         " +sss );
                    bundle = new Bundle();
                    //bundle.putInt("max_id",ans);
                    bundle.putString("Re_to_myinfo",sss);
                    intent.putExtras(bundle);
                    startActivity(intent);
                    break;
                case R.id.add_x:
                    intent = new Intent(RecyclerviewActivity.this, Publish_b.class);
                    Log.d("--Re 发送到 Publish 的值-- ", "--  is         " +sss );
                    bundle = new Bundle();
                    bundle.putInt("max_id",ans);
                    bundle.putString("Re_to_add",sss);
                    intent.putExtras(bundle);
                    startActivity(intent);
                    break;
            }
        }
    }
}